from flask import request

from db.dao import StockInfoDao, IndustrySectorFundsDao
from db.models import IndustrySectorFund
from web import custom


@custom.route('/market')
def market1():
    # p = Process(target=market)
    # p.start()
    # p.join()
    return 'ok'


@custom.route('/industry')
def industry1():
    # p = Process(target=industry)
    # p.start()
    # p.join()
    return 'ok'


@custom.route('/stock_info')
def get_stock_info():
    '''
    获取股票基础信息
    :return:
    '''
    # p_list = []
    # industry_list = []
    # industry_name = request.args.get('industry_name')
    # if industry_name:
    #     industry = IndustryInfoDao().findByIndustryName(industry_name)
    #     if industry:
    #         industry_list.append(industry)
    # else:
    #     industry_list = IndustryInfoDao().findAll()
    # if industry_list:
    #     chunks_list = ObjDictTool.chunks(industry_list, 3)
    #     for chunk in chunks_list:
    #         # p = Process(target=stockInfo, args=tuple(chunk))
    #         # p.start()
    #         # p_list.append(p)
    #     for p1 in p_list:
    #         p1.join()

    return 'ok'


@custom.route('/save-stock_info', methods=['POST'])
def insert_stock_info():
    # start_url = request.form.get('stock_detail_url')
    # if start_url:
    # p = Process(target=stockInfoSingle, args=(start_url,))
    # p.start()
    # p.join()

    return 'ok'


@custom.route('/import_industry_info')
def import_industry_info():
    '''
    获取股票基础信息
    :return:
    '''
    # p = Process(target=importIndustryInfo)
    # p.start()
    # p.join()
    return 'ok'


@custom.route('/stock_funds_1')
def get_stock_funds_1():
    '''
    获取股票资金涨幅情况
    :return:
    '''

    # p_list = []
    #
    # stock_list = StockInfoDao().findNoSpiderStocks(create_date=HolidayUtil.getLastTradingDay(datetime.datetime.now()),
    #                                                stock_funds_1=1, stock_funds_2=None)
    # if stock_list:
    # chunks_list = ObjDictTool.chunks(stock_list, 3)
    # for chunk in chunks_list:
    # p = Process(target=stock_funds_1, args=tuple(chunk))
    # p.start()
    # p_list.append(p)
    # for p1 in p_list:
    #     p1.join()
    return 'ok'


@custom.route('/stock_funds_2')
def get_stock_funds_2():
    '''
    获取股票资金涨幅情况
    :return:
    '''
    p_list = []

    # stock_list = StockInfoDao().findNoSpiderStocks(create_date=HolidayUtil.getLastTradingDay(datetime.datetime.now()),
    #                                                stock_funds_1=None, stock_funds_2=1)
    # if stock_list:
    #     chunks_list = ObjDictTool.chunks(stock_list, 3)
    #     for chunk in chunks_list:
    #         p = Process(target=stock_funds_2, args=tuple(chunk))
    #         p.start()
    #         p_list.append(p)
    #     for p1 in p_list:
    #         p1.join()
    return 'ok'


@custom.route('/stock_funds')
def import_stock_funds():
    '''
    获取股票资金涨幅情况
    :return:
    '''
    args_get = request.args.get('stock_code')
    if not args_get:
        return 'ok'

    stock = StockInfoDao().findByStockCode(args_get)
    chunk = []
    chunk.append(stock)

    # if stock:
    #     p = Process(target=stock_funds_1, args=tuple(chunk))
    #     p1 = Process(target=stock_funds_2, args=tuple(chunk))
    #     p1.start()
    #     p.start()
    #     p.join()
    #     p1.join()

    return 'ok'


@custom.route('/stock_funds_batch')
def import_stock_funds_batch():
    '''
    获取股票资金涨幅情况
    :return:
    '''
    stock_codes = request.args.get('stock_code')
    if stock_codes:
        stockList = StockInfoDao().findByStockCodeBatch(stock_codes.split(','))
    industry_name = request.args.get('industry_name')
    if industry_name:
        stockList = StockInfoDao().findByIndustry(industry_name)

    # if stockList:
    #     p = Process(target=stock_funds_1, args=tuple(stockList))
    #     p1 = Process(target=stock_funds_2, args=tuple(stockList))
    #     p1.start()
    #     p.start()
    #     p.join()
    #     p1.join()
    if industry_name:
        id = request.args.get('id')
        industry_sector_fund = IndustrySectorFund()
        setattr(industry_sector_fund, 'id', id)
        setattr(industry_sector_fund, 'stock_imported', 1)
        IndustrySectorFundsDao().updateById(industry_sector_fund)
    return 'ok'


@custom.route('/remove-stock-batch')
def remove_stock_batch():
    '''
    获取股票资金涨幅情况
    :return:
    '''
    args_get = request.args.get('ids')
    if not args_get:
        return 'ok'
    StockInfoDao().removeStockBatch(args_get.split(','))
    return 'ok'


@custom.route('/import-stock-funds')
def importStockFunds():
    stockList = StockInfoDao().findAll()

    # if stockList:
    #     p_list = []
    #     chunks_list = ObjDictTool.chunks(stockList, 3)
    #     for chunk in chunks_list:
    #         p = Process(target=importStockFundamental, args=tuple(chunk))
    #         p.start()
    #         p_list.append(p)
    #     for p1 in p_list:
    #         p1.join()
    return 'ok'


@custom.route('/import-news-industy-guid')
def importIndustryGuidNews():
    # p = Process(target=importIndustryGuid)
    # p.start()
    # p.join()
    return 'ok'


@custom.route('/import-news-industy-research')
def importIndustryResearchNews():
    # p = Process(target=importIndustryResearch)
    # p.start()
    # p.join()
    return 'ok'


@custom.route('/import-news-industy-info')
def importIndustryInfoNews():
    # p = Process(target=importIndustryInformation)
    # p.start()
    # p.join()
    return 'ok'
